import React, { useImperativeHandle } from "react";
import { Form, Input, Radio, InputNumber } from 'antd';

import type { IOuterCreateOrEditProps } from "@yaf/pro-ui";
import { getEnv  } from "@/constants";


const layout = {
  labelCol: { span: 6 },
  wrapperCol: { span: 13 },
};

export const CreateOrEdit: IOuterCreateOrEditProps["CreateOrEditContent"] = React.forwardRef(({ values }, actionRef) => {
  const { commonStatus, commonStatusList } = getEnv().globalConfig;
  const [form] = Form.useForm();
  useImperativeHandle(actionRef, () => ({
    getFormData: () => new Promise((resolve, reject) => {
      form.validateFields().then((data) => {
        resolve(data);
      }).catch(() => {
        reject(new Error("请检查参数"));
      });
    })
  }));

  return <Form
    {...layout}
    initialValues={{
      status: commonStatus.normal,
      dictSort: 0,
      isDefault: 0,
      ...values
    }}
    form={form}
  >
    <Form.Item
      label="数据标签"
      name="dictLabel"
      rules={[{ required: true, message: '请填写数据标签!' }]}
    >
      <Input />
    </Form.Item>
    <Form.Item
      label="数据键值"
      name="dictValue"
      rules={[{ required: true, message: '请填写数据键值!' }]}
    >
      <Input />
    </Form.Item>
    <Form.Item
      label="显示排序"
      name="dictSort"
      rules={[{ required: true, message: '请填写显示排序!' }]}
    >
      <InputNumber min={0} />
    </Form.Item>
    <Form.Item
      label="状态"
      name="status"
    >
      <Radio.Group options={commonStatusList} />
    </Form.Item>

    <Form.Item
      label="系统默认"
      name="isDefault"
    >
      <Radio.Group options={[
        { label: "是", value: 1 },
        { label: "否", value: 0 },
      ]} />
    </Form.Item>

    <Form.Item label="备注" name="remark">
      <Input.TextArea />
    </Form.Item>
  </Form>

});
